<?php

class Api{
    
    protected static $VCB_url = 'http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx';


    public static function getCurrency($currencyCode='USD')
    {
        $hasFound = false;
        $data = Api::getDataByUrl(self::$VCB_url);
        $xml = simplexml_load_string($data);
        
        $result = array();
        
        foreach($xml -> children() as $child)
        {
            foreach($child -> attributes() as $a => $b)
            {
                $result[$a] = $b;
                if($a == 'CurrencyCode' && $b == $currencyCode)
                {
                    $hasFound = true;
                }
            }
            
            if($hasFound) break;
        }
        
        return $result;
    }
    
    public static function getDataByUrl($url)
    {
        $ch = curl_init();
            
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        
        curl_close($ch);  
        
        return $output;
    }
}
?>
